
#include "Renderer.h"

using namespace std;

int main(int argc, char* argv[])
{
    auto window = make_shared<Window>("RenderTest", 800, 600);
    Renderer r(window);

    auto model = std::make_shared<TriangleModel>();
    model->load("../assets/models/nanosuit/nanosuit.obj");
    auto t = glm::translate(glm::mat4(1.f), glm::vec3(0.f, -0.7f, 0.f));
    auto s = glm::scale(glm::mat4(1.f), glm::vec3(0.5f));
    model->transform = t * s;

    r.submit(model);

    auto light = std::make_shared<Light>();
    light->position = Vector3f(-6.f, 8.f, 10.f);
    light->intensity = Vector3f(10.f);
    r.submit(light);

    {
        float vertices[] = {
            // Back face
            -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f,  0.0f, // Bottom-left
             0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f,  1.0f, // top-right
             0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f,  0.0f, // bottom-right         
             0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f,  1.0f, // top-right
            -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f,  0.0f, // bottom-left
            -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f,  1.0f, // top-left
            // Front face
            -0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  0.0f,  0.0f, // bottom-left
             0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  1.0f,  0.0f, // bottom-right
             0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  1.0f,  1.0f, // top-right
             0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  1.0f,  1.0f, // top-right
            -0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  0.0f,  1.0f, // top-left
            -0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  0.0f,  0.0f, // bottom-left
            // Left face
            -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  0.0f, // top-right
            -0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f, // top-left
            -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  1.0f, // bottom-left
            -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  1.0f, // bottom-left
            -0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f, // bottom-right
            -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  0.0f, // top-right
            // Right face
             0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,  1.0f,  0.0f, // top-left
             0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,  0.0f,  1.0f, // bottom-right
             0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,  1.0f,  1.0f, // top-right         
             0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,  0.0f,  1.0f, // bottom-right
             0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,  1.0f,  0.0f, // top-left
             0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,  0.0f,  0.0f, // bottom-left     
            // Bottom face
            -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,  0.0f,  1.0f, // top-right
             0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,  1.0f,  1.0f, // top-left
             0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,  1.0f,  0.0f, // bottom-left
             0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,  1.0f,  0.0f, // bottom-left
            -0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,  0.0f,  0.0f, // bottom-right
            -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,  0.0f,  1.0f, // top-right
            // Top face
            -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f,  1.0f, // top-left
             0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  1.0f,  0.0f, // bottom-right
             0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f, // top-right     
             0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  1.0f,  0.0f, // bottom-right
            -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f,  1.0f, // top-left
            -0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  0.0f,  0.0f  // bottom-left   
        };

        TriangleMesh mesh;
        auto model = std::make_shared<TriangleModel>();
        mesh.load(vertices, sizeof(vertices) / sizeof(float) / 8, Material::DefaultDiffuseMaterial());
        model->meshes.push_back(mesh);
        model->recalculateBounds();

        // r.submit(model);
    }

    {
        float vertices[] = {
            // Bottom face
            -15.f, -0.7f, -15.f,  0.0f,  1.0f,  0.0f,  0.0f,  1.0f, // top-right
             15.f, -0.7f, -15.f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f, // top-left
             15.f, -0.7f,  15.f,  0.0f,  1.0f,  0.0f,  1.0f,  0.0f, // bottom-left
             15.f, -0.7f,  15.f,  0.0f,  1.0f,  0.0f,  1.0f,  0.0f, // bottom-left
            -15.f, -0.7f,  15.f,  0.0f,  1.0f,  0.0f,  0.0f,  0.0f, // bottom-right
            -15.f, -0.7f, -15.f,  0.0f,  1.0f,  0.0f,  0.0f,  1.0f, // top-right
        };

        TriangleMesh mesh;
        auto model = std::make_shared<TriangleModel>();
        mesh.load(vertices, sizeof(vertices) / sizeof(float) / 8, Material::DefaultDiffuseMaterial());
        model->meshes.push_back(mesh);
        model->recalculateBounds();

        r.submit(model);
    }

    while (window->isRunning()) {
        r.render();
    }
    
    return 0;
}
